<?php
//require_once 'dr-app-config.php';
//echo '<pre>';

function dr_db_get_option($option, $default = false)
{
	global $db;

	$option = trim($option);
	if (empty($option)) {
		return false;
	}

	$sql = 'SELECT `option_value` FROM ^options where `option_key` = $';
	$result = dr_db_query_sub($db, $sql, $option);

	if ($row = mysql_fetch_assoc($result)) {
		return $row['option_value'];
	} else {
		return $default;
	}

}

//var_dump(dr_db_get_option('sitename'));
//var_dump(dr_db_get_option('aaaa'));

function dr_db_load_all_options()
{
	global $db;
	$sql = "SELECT `option_key`, `option_value` FROM ^options";
	$result = dr_db_query_sub($db, $sql);
	$array_result = array();
	while ($row = mysql_fetch_assoc($result)) {
		$array_result[$row['option_key']] = $row['option_value'];
	}
	return $array_result;
}

//var_dump(dr_db_load_all_options());

function dr_db_add_option($option, $value = '', $autoload = 1)
{
	global $db;
	$option = trim($option);
	if (empty($option)) {
		return false;
	}
	$sql = 'INSERT INTO ^options (`option_key`, `option_value`, `autoload`) VALUES ($, $, $) ON DUPLICATE KEY UPDATE `option_key` = VALUES(`option_key`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)';
	$result = dr_db_query_sub($db, $sql, $option, $value, $autoload);

	if ($result) {
		return true;
	}
	return false;
}

//var_dump(dr_db_add_option('xxxx', 'yyyy'));

function dr_db_update_option($option, $new_value)
{
	global $db;

	$option = trim($option);
	if (empty($option)) {
		return false;
	}

	$old_value = dr_db_get_option($option);
	if ($new_value === $old_value) {
		return true;
	}

	if (false === $old_value) {
		return dr_db_add_option($option, $new_value);
	}

	$sql = 'UPDATE `^options` set `option_value` = $ WHERE `option_key` = $';
	$result = dr_db_query_sub($db, $sql, $new_value, $option);
	if ($result) {
		return true;
	}
	return false;
}

//var_dump(dr_db_update_option('xxxxx', 'zzzz'));

function dr_db_delete_option($option)
{
	global $db;
	$sql = 'DELETE FROM `^options` WHERE `option_key` = $';
	$result = dr_db_query_sub($db, $sql, $option);
	if ($result) {
		return true;
	}
	return false;
}

//var_dump(dr_db_delete_option('xxxxx'));
# XXX 考虑将类似的变量统一到一起，便于管理
$general_settings = array('sitename', 'question_per_page', 'answer_per_page', 'days_to_display', 'display_day_top', 'display_week_top',
                          'display_month_top', 'display_all_top', 'question_top_num', 'tag_top_num', 'user_top_num');
function dr_db_get_general_settings()
{
	global $general_settings;
	$all_options = dr_db_load_all_options();
	$result = array();
	foreach ($general_settings as $setting) {
		if (isset($all_options[$setting])) {
			$result[$setting] = $all_options[$setting];
		} else {
			$result[$setting] = '';
		}
	}
	return $result;
}

//var_dump(dr_db_get_general_settings());

function dr_db_update_general_settings($options)
{
	foreach ($options as $key => $value) {
		$result = dr_db_update_option($key, $value);
		if (!$result) {
			return false;
		}
	}
	return true;
}
//var_dump(dr_db_update_general_settings(array('sitename' => 'iDaren')));
